<script setup lang="ts">
import AddWrite from './components/addWrite.vue'
import { ContentDetailWrap } from '@/components/ContentDetailWrap'
import { ref, unref } from 'vue'
import { ElButton } from 'element-plus'
import { useI18n } from '@/hooks/web/useI18n'
import { useRouter } from 'vue-router'
import { purchaseAdd } from '@/api/common'
import { purchaseType } from '@/api/common/types'
import { useEmitt } from '@/hooks/web/useEmitt'

const { emitter } = useEmitt()

const { push } = useRouter()

const { t } = useI18n()

const addWrite = ref<ComponentRef<typeof AddWrite>>()

const loading = ref(false)

const save = async () => {
  const write = unref(addWrite)
  console.log('---addWrite---', write?.getFormData())

  const data = {
    goods_id: write?.getFormData()?.typeOfGoods,
    store_id: write?.getFormData()?.locationOfGoods,
    transport: write?.getFormData()?.modeOfTransport,
    placestart: write?.getFormData()?.origin,
    client_code: write?.getFormData()?.relatedCustomers,
    worth: write?.getFormData()?.value,
    servicefee: write?.getFormData()?.serviceCharge,
    freight: write?.getFormData()?.collectionOfFreight,
    rebate: write?.getFormData()?.factoryRebate,
    chajia: write?.getFormData()?.priceDifferenceOfGoods,
    remark: write?.getFormData()?.productNameRemarks,
    piece: write?.getFormData()?.warehousingItems,
    buytime: write?.getFormData()?.warehousingTime
  } as purchaseType
  // data.buytime = '' + parseInt(data.buytime) / 1000
  console.log('---采购单新增参数---', data)
  const res = await purchaseAdd(data)
  if (res) {
    emitter.emit('getList', 'add')
    push('/logistics/purchaseOrder')
  }
}
</script>

<template>
  <ContentDetailWrap :title="t('exampleDemo.add')" @back="push('/logistics/purchaseOrder')">
    <!-- <Write ref="writeRef" /> -->
    <AddWrite ref="addWrite" />

    <template #right>
      <ElButton type="primary" :loading="loading" @click="save">
        {{ t('exampleDemo.save') }}
      </ElButton>
    </template>
  </ContentDetailWrap>
</template>
